package com.youlu.campus.common.utils;

import java.security.SecureRandom;
import java.util.Random;

/**
 * @Description: RandomNumUtil
 * @Date 2025/7/14
 * @module RandomNumUtil
 * @author: su
 */
public class RandomStringUtil {
    // 定义可用字符集：大小写字母和数字
    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static final SecureRandom RANDOM = new SecureRandom();

    /**
     * 生成指定长度的随机字符串
     *
     * @param length 字符串长度
     * @return 随机字符串
     */
    public static String generateRandomString(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("长度必须为正数");
        }
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            // 随机获取字符集中的字符
            int randomIndex = RANDOM.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(randomIndex));
        }
        return sb.toString();
    }

    public static String getRandNum(int charCount) {
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < charCount; i++) {
            int num = r.nextInt(10);
            sb.append(num);
        }
        return sb.toString();
    }

}
